#include <stdio.h>
#include <string.h>
#include "nvs_flash.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#define NAME_SPACE_WIFI_1   "wifi1"
#define NAME_SPACE_WIFI_2   "wifi2"
#define NVS_SSID_KEY        "ssid"
#define NVS_PASSWORD_KEY    "password"

static const char* TAG = "nvs_test";

void app_main(void)//main函数在ESP-IDF就是一个任务函数；
{
    //初始化NVS分区
    esp_err_t ret = nvs_flash_init();
    if(ret == ESP_FAIL){
        nvs_flash_erase();
        ESP_ERROR_CHECK(nvs_flash_init());
    }

    nvs_handle_t wifi1_nvs_handle;
    ret = nvs_open(NAME_SPACE_WIFI_1, NVS_READWRITE, &wifi1_nvs_handle);//设置并打开命名空间
    if(ret != ESP_OK){
        ESP_LOGE(TAG, "open NAME_SPACE_WIFI_1 fail");
    }
    else{
        nvs_set_blob(wifi1_nvs_handle, NVS_SSID_KEY, "starry", strlen("starry"));
        nvs_set_blob(wifi1_nvs_handle, NVS_PASSWORD_KEY, "12345", strlen("12345"));       
    }
    nvs_commit(wifi1_nvs_handle);
    nvs_close(wifi1_nvs_handle);

    nvs_handle_t wifi2_nvs_handle;
    ret = nvs_open(NAME_SPACE_WIFI_2, NVS_READWRITE, &wifi2_nvs_handle);//设置并打开命名空间
    if(ret != ESP_OK){
        ESP_LOGE(TAG, "open NAME_SPACE_WIFI_2 fail");
    }
    else{
        nvs_set_blob(wifi2_nvs_handle, NVS_SSID_KEY, "zero", strlen("zero"));
        nvs_set_blob(wifi2_nvs_handle, NVS_PASSWORD_KEY, "54321", strlen("54321"));
    }
    nvs_commit(wifi2_nvs_handle);
    nvs_close(wifi2_nvs_handle);

    vTaskDelay(1000/portTICK_PERIOD_MS);

    nvs_open(NAME_SPACE_WIFI_1, NVS_READONLY, &wifi1_nvs_handle);
    nvs_open(NAME_SPACE_WIFI_2, NVS_READONLY, &wifi2_nvs_handle);

    size_t length = 0;
    nvs_get_blob(wifi1_nvs_handle, NVS_SSID_KEY, NULL, &length);
    if(length > 0){
        char* wifi1_ssid = (char*)malloc((length+1)*sizeof(char));
        memset(wifi1_ssid, 0, strlen(wifi1_ssid));
        nvs_get_blob(wifi1_nvs_handle, NVS_SSID_KEY, wifi1_ssid, &length);
        ESP_LOGI(TAG, "wifi1_ssid is %s", wifi1_ssid);
        free(wifi1_ssid);
    }

    nvs_get_blob(wifi1_nvs_handle, NVS_PASSWORD_KEY, NULL, &length);
    if(length > 0){
        char* wifi1_password = (char*)malloc((length+1)*sizeof(char));
        memset(wifi1_password, 0, strlen(wifi1_password));
        nvs_get_blob(wifi1_nvs_handle, NVS_PASSWORD_KEY, wifi1_password, &length);
        ESP_LOGI(TAG, "wifi1_password is %s", wifi1_password);
        free(wifi1_password);//用完要及时释放
    }

    nvs_get_blob(wifi2_nvs_handle, NVS_SSID_KEY, NULL, &length);
    if(length > 0){
        char* wifi2_ssid = (char*)malloc((length+1)*sizeof(char));
        memset(wifi2_ssid, 0, strlen(wifi2_ssid));
        nvs_get_blob(wifi2_nvs_handle, NVS_SSID_KEY, wifi2_ssid, &length);
        ESP_LOGI(TAG, "wifi2_ssid is %s", wifi2_ssid);
        free(wifi2_ssid);
    }

    nvs_get_blob(wifi2_nvs_handle, NVS_PASSWORD_KEY, NULL, &length);
    if(length > 0){
        char* wifi2_password = (char*)malloc((length+1)*sizeof(char));
        memset(wifi2_password, 0, strlen(wifi2_password));
        nvs_get_blob(wifi2_nvs_handle, NVS_PASSWORD_KEY, wifi2_password, &length);
        ESP_LOGI(TAG, "wifi2_password is %s", wifi2_password);
        free(wifi2_password);
    }    

    nvs_close(wifi1_nvs_handle);
    nvs_close(wifi2_nvs_handle);

    vTaskDelay(1000/portTICK_PERIOD_MS);
}
